/*天气信息*/
void get_weather(void){
    String city = actual.city;
    String tubiao = actual.weather_name;
    Serial.println(city+":"+tubiao);
    if(tubiao=="多云"||tubiao=="晴间多云"||tubiao=="大部多云"){
      display.drawInvertedBitmap(280, 15, duoyun, 128, 128, GxEPD_BLACK);//画图
    }else if(tubiao=="晴"){
      display.drawInvertedBitmap(280, 15, qing, 128, 128, GxEPD_BLACK);//画图
    }else if(tubiao=="阴"){
      display.drawInvertedBitmap(280, 15, yin, 128, 128, GxEPD_BLACK);//画图
    }else if(tubiao=="阵雨"){
      display.drawInvertedBitmap(280, 15, zhenyu, 128, 128, GxEPD_BLACK);//画图
    }else if(tubiao=="雷阵雨"){
      display.drawInvertedBitmap(280, 15, leizhenyu, 128, 128, GxEPD_BLACK);//画图
    }else if(tubiao=="雷阵雨伴有冰雹"){
      display.drawInvertedBitmap(280, 15, leizhenyubanyoubingbao, 128, 128, GxEPD_BLACK);//画图
    }else if(tubiao=="小雨"){
      display.drawInvertedBitmap(280, 15, xiaoyu, 128, 128, GxEPD_BLACK);//画图
    }else if(tubiao=="中雨"){
      display.drawInvertedBitmap(280, 15, zhongyu, 128, 128, GxEPD_BLACK);//画图
    }else if(tubiao=="大雨"){
      display.drawInvertedBitmap(280, 15, dayu, 128, 128, GxEPD_BLACK);//画图
    }else if(tubiao=="暴雨"||tubiao=="大暴雨"||tubiao=="特大暴雨"){
      display.drawInvertedBitmap(280, 15, baoyu, 128, 128, GxEPD_BLACK);//画图
    }else if(tubiao=="冻雨"){
      display.drawInvertedBitmap(280, 15, dongyu, 128, 128, GxEPD_BLACK);//画图
    }else if(tubiao=="雨夹雪"){
      display.drawInvertedBitmap(280, 15, yujiaxue, 128, 128, GxEPD_BLACK);//画图
    }else if(tubiao=="阵雪"){
      display.drawInvertedBitmap(280, 15, zhenxue, 128, 128, GxEPD_BLACK);//画图
    }else if(tubiao=="小雪"){
      display.drawInvertedBitmap(280, 15, xiaoxue, 128, 128, GxEPD_BLACK);//画图
    }else if(tubiao=="中雪"){
      display.drawInvertedBitmap(280, 15, zhongxue, 128, 128, GxEPD_BLACK);//画图
    }else if(tubiao=="大雪"){
      display.drawInvertedBitmap(280, 15, daxue, 128, 128, GxEPD_BLACK);//画图
    }else if(tubiao=="暴雪"){
      display.drawInvertedBitmap(280, 15, baoxue, 128, 128, GxEPD_BLACK);//画图
    }else if(tubiao=="浮尘"){
      display.drawInvertedBitmap(280, 15, fuchen, 128, 128, GxEPD_BLACK);//画图
    }else if(tubiao=="扬沙"||tubiao=="沙尘暴"||tubiao=="强沙尘暴"){
      display.drawInvertedBitmap(280, 15, shachenbao, 128, 128, GxEPD_BLACK);//画图
    }else if(tubiao=="雾"){
      display.drawInvertedBitmap(280, 15, dawu, 128, 128, GxEPD_BLACK);//画图
    }else if(tubiao=="霾"){
      display.drawInvertedBitmap(280, 15, mai, 128, 128, GxEPD_BLACK);//画图
    }else if(tubiao=="未知"){
      display.drawInvertedBitmap(280, 15, weizhi, 128, 128, GxEPD_BLACK);//画图
    }
    
    /*位置+温度*/
  display.drawInvertedBitmap(280, 128, weizhi, 32, 28, GxEPD_RED);//画图
  u8g2Fonts.setCursor(312, 150);
  u8g2Fonts.setFont(chinese_city_gb2312);
  u8g2Fonts.setForegroundColor(GxEPD_BLACK);  // 设置前景色
  u8g2Fonts.setBackgroundColor(GxEPD_WHITE);  // 设置背景色
  u8g2Fonts.print(String(actual.city));
//  u8g2Fonts.setCursor(283,222);
    u8g2Fonts.setCursor(272,242);
    u8g2Fonts.print("今天");
    u8g2Fonts.setCursor(272,262);
    u8g2Fonts.print("明天");
    u8g2Fonts.setCursor(272,282);
    u8g2Fonts.print("后天");
    String text_day0, text_night0, dn0_s;
    String text_day1, text_night1, dn1_s;
    String text_day2, text_night2, dn2_s;
    const char* dn0; const char* dn1; const char* dn2;

    if (strcmp(future.date0_text_day, future.date0_text_night) != 0) //今天
    {
      text_day0 = future.date0_text_day;
      text_night0 = future.date0_text_night;
      dn0_s = text_day0 + "转" + text_night0;
#define LIMIT_WATHER_LEN   0 //( 10 )
#if (LIMIT_WATHER_LEN > 0)
      if(dn0_s.length()> LIMIT_WATHER_LEN){
        String temp = "";
        for(int j=0; j<LIMIT_WATHER_LEN; j++){
          temp += dn0_s[j];
        }
        dn0_s = temp;
      }
#endif
      dn0 = dn0_s.c_str();
      u8g2Fonts.setCursor(300, 242);
      u8g2Fonts.print(dn0);
    }
    else
    {
      dn0 = future.date0_text_night;
      u8g2Fonts.setCursor(300, 242);
      u8g2Fonts.print(dn0);
    }

    if (strcmp(future.date1_text_day, future.date1_text_night) != 0) //明天
    {
      text_day1 = future.date1_text_day;
      text_night1 = future.date1_text_night;
      dn1_s = text_day1 + "转" + text_night1;
#if (LIMIT_WATHER_LEN > 0)
      if(dn1_s.length()> LIMIT_WATHER_LEN){
        String temp = "";
        for(int j=0; j<LIMIT_WATHER_LEN; j++){
          temp += dn1_s[j];
        }
        dn1_s = temp;
      }
#endif
      dn1 = dn1_s.c_str();
      u8g2Fonts.setCursor(300, 262);
      u8g2Fonts.print(dn1);
    }
    else
    {
      dn1 = future.date1_text_night;
      u8g2Fonts.setCursor(300, 262);
      u8g2Fonts.print(dn1);
    }

    if (strcmp(future.date2_text_day, future.date2_text_night) != 0) //后天
    {
      text_day2 = future.date2_text_day;
      text_night2 = future.date2_text_night;
      dn2_s = text_day2 + "转" + text_night2;
#if (LIMIT_WATHER_LEN > 0)
      if(dn2_s.length()> LIMIT_WATHER_LEN){
        String temp = "";
        for(int j=0; j<LIMIT_WATHER_LEN; j++){
          temp += dn2_s[j];
        }
        dn2_s = temp;
      }
#endif
      dn2 = dn2_s.c_str();
      u8g2Fonts.setCursor(300, 282);
      u8g2Fonts.print(dn2);
    }
    else
    {
      dn2 = future.date2_text_night;
      u8g2Fonts.setCursor(300, 282);
      u8g2Fonts.print(dn2);
    }
  //显示高低温
    String  high0, high1, high2, low0, low1, low2, hl0_s, hl1_s, hl2_s;
    high0 = future.date0_high; high1 = future.date1_high; high2 = future.date2_high;
    low0 = future.date0_low; low1 = future.date1_low; low2 = future.date2_low;
    hl0_s = high0 + "/" + low0;
    hl1_s = high1 + "/" + low1;
    hl2_s = high2 + "/" + low2;
    const char* hl0 = hl0_s.c_str();
    const char* hl1 = hl1_s.c_str();
    const char* hl2 = hl2_s.c_str();
    u8g2Fonts.setCursor(373,242);
    u8g2Fonts.print(hl0);
    u8g2Fonts.setCursor(373,262);
    u8g2Fonts.print(hl1);
    u8g2Fonts.setCursor(373,282);
    u8g2Fonts.print(hl2);
  
}
#define STD_TIMEZONE_OFFSET +8 
/*get time*/

/*时间日期数字格式化*/
  String pressNum(int num) {
    if (num < 10 )
      return "0" + String(num);
    else
      return String(num);
  }
void updatetime() {
  String timedate,timetim="";
  int weekdays, days, months,years,minutes,hours; 
  TimeChangeRule *tcr;        // Pointer to the time change rule
  time_t utc = now();
  TimeChangeRule mySTD = {"", First,  Sun, Jan, 0, STD_TIMEZONE_OFFSET * 60};
  Timezone myTZ(mySTD, mySTD);
  time_t localTime = myTZ.toLocal(utc, &tcr);
  weekdays = weekday(localTime);
  Serial.print("周");
  Serial.println(weekdays);
//  if(weekdays!=7){
//    weekdays-=1;
//  }else{
//    weekdays=1;
//  }
  days = day(localTime);
  months = month(localTime);
  years = year(localTime);
  timedate+=String(pressNum(months)) ;
  timedate+= "月" ;
  
//  if(weekdays==0) weekdays=7;
  minutes =   minute(localTime);
  hours   =   hour(localTime) ;   //12 hour format use : hourFormat12(localTime)  isPM()/isAM()
  timetim="星期";
  if(weekdays==2)timetim+="一"; 
  else if(weekdays==3)timetim+="二"; 
  else if(weekdays==4)timetim+="三"; 
  else if(weekdays==5)timetim+="四"; 
  else if(weekdays==6)timetim+="五"; 
  else if(weekdays==7)timetim+="六"; 
  else if(weekdays==1)timetim+="天"; 
  timetim+=" ";
  timetim+=String(hours);
  timetim+=" 点 ";
  if(minutes<10){
    timetim+="0";
   }
  timetim+=String(minutes);
  Serial.print(String(years)+"-"+String(months)+"-"+String(days)+"-");
  Serial.println(timetim);
  
  if(days<10){
    display.drawInvertedBitmap(301, 144, ling, 48, 66, GxEPD_RED);//画图
    if(days==1){
      display.drawInvertedBitmap(349, 144, yi, 18, 66, GxEPD_RED);//画图
    }else if(days==2){
      display.drawInvertedBitmap(349, 144, er, 48, 66, GxEPD_RED);//画图
    }else if(days==3){
      display.drawInvertedBitmap(349, 144, san, 48, 66, GxEPD_RED);//画图
    }else if(days==4){
      display.drawInvertedBitmap(349, 144, si, 48, 66, GxEPD_RED);//画图
    }else if(days==5){
      display.drawInvertedBitmap(349, 144, wu, 48, 66, GxEPD_RED);//画图
    }else if(days==6){
      display.drawInvertedBitmap(349, 144, liu, 48, 66, GxEPD_RED);//画图
    }else if(days==7){
      display.drawInvertedBitmap(349, 144, qi, 48, 66, GxEPD_RED);//画图
    }else if(days==8){
      display.drawInvertedBitmap(349, 144, ba, 48, 66, GxEPD_RED);//画图
    }else if(days==9){
      display.drawInvertedBitmap(349, 144, jiu, 48, 66, GxEPD_RED);//画图
    }
    
  }else if(days>=10&&days<20){
      display.drawInvertedBitmap(301, 144, yi, 18, 66, GxEPD_RED);//画图
    if(days==10){
      display.drawInvertedBitmap(349, 144, ling, 48, 66, GxEPD_RED);//画图
    }else if(days==11){
      display.drawInvertedBitmap(349, 144, yi, 18, 66, GxEPD_RED);//画图
    }else if(days==12){
      display.drawInvertedBitmap(349, 144, er, 48, 66, GxEPD_RED);//画图
    }else if(days==13){
      display.drawInvertedBitmap(349, 144, san, 48, 66, GxEPD_RED);//画图
    }else if(days==14){
      display.drawInvertedBitmap(349, 144, si, 48, 66, GxEPD_RED);//画图
    }else if(days==15){
      display.drawInvertedBitmap(349, 144, wu, 48, 66, GxEPD_RED);//画图
    }else if(days==16){
      display.drawInvertedBitmap(349, 144, liu, 48, 66, GxEPD_RED);//画图
    }else if(days==17){
      display.drawInvertedBitmap(349, 144, qi, 48, 66, GxEPD_RED);//画图
    }else if(days==18){
      display.drawInvertedBitmap(349, 144, ba, 48, 66, GxEPD_RED);//画图
    }else if(days==19){
      display.drawInvertedBitmap(349, 144, jiu, 48, 66, GxEPD_RED);//画图
    }
  }else if(days>=20&&days<30){
    display.drawInvertedBitmap(301, 144, er, 48, 66, GxEPD_RED);//画图
    if(days==20){
      display.drawInvertedBitmap(349, 144, ling, 48, 66, GxEPD_RED);//画图
    }else if(days==21){
      display.drawInvertedBitmap(349, 144, yi, 18, 66, GxEPD_RED);//画图
    }else if(days==22){
      display.drawInvertedBitmap(349, 144, er, 48, 66, GxEPD_RED);//画图
    }else if(days==23){
      display.drawInvertedBitmap(349, 144, san, 48, 66, GxEPD_RED);//画图
    }else if(days==24){
      display.drawInvertedBitmap(349, 144, si, 48, 66, GxEPD_RED);//画图
    }else if(days==25){
      display.drawInvertedBitmap(349, 144, wu, 48, 66, GxEPD_RED);//画图
    }else if(days==26){
      display.drawInvertedBitmap(349, 144, liu, 48, 66, GxEPD_RED);//画图
    }else if(days==27){
      display.drawInvertedBitmap(349, 144, qi, 48, 66, GxEPD_RED);//画图
    }else if(days==28){
      display.drawInvertedBitmap(349, 144, ba, 48, 66, GxEPD_RED);//画图
    }else if(days==29){
      display.drawInvertedBitmap(349, 144, jiu, 48, 66, GxEPD_RED);//画图
    }
  }else{
      display.drawInvertedBitmap(301, 144, san, 48, 66, GxEPD_RED);//画图
    if(days==30){
      display.drawInvertedBitmap(349, 144, ling, 48, 66, GxEPD_RED);//画图
    }else if(days==31){
      display.drawInvertedBitmap(349, 144, yi, 18, 66, GxEPD_RED);//画图
    }
  }
  u8g2Fonts.setFont(chinese_city_gb2312);
  u8g2Fonts.setForegroundColor(GxEPD_BLACK);  // 设置前景色
  u8g2Fonts.setBackgroundColor(GxEPD_WHITE);  // 设置背景色
  u8g2Fonts.setCursor(300, 30);//标记：位置需要修改
  u8g2Fonts.print(timetim);
}
void hitokoto(){
//  display.drawRect(0, 0, 38, 12,1);
  u8g2Fonts.setFont(chinese_city_gb2312);
  u8g2Fonts.setForegroundColor(GxEPD_RED);  // 设置前景色
  u8g2Fonts.setBackgroundColor(GxEPD_WHITE);  // 设置背景色
  u8g2Fonts.setCursor(42, 12);
  u8g2Fonts.print(String(tips.hitokoto));
  u8g2Fonts.setCursor(8, 13);
  u8g2Fonts.print(String("TIPS: "));
  //display.drawInvertedBitmap(0, 0, tipsp, 38, 13, GxEPD_RED);//画图  
}
void newsdisplay(){
  display.drawLine(269, 25, 269, 296, 0);
  u8g2Fonts.setFont(chinese_city_gb2312);
  u8g2Fonts.setForegroundColor(GxEPD_BLACK);  // 设置前景色
  u8g2Fonts.setBackgroundColor(GxEPD_WHITE);  // 设置背景色
  if(udc%3==0){
    display.drawInvertedBitmap(75, 16, weibo, 32, 32, GxEPD_BLACK);//画图
    u8g2Fonts.setCursor(106,38);
    u8g2Fonts.print("微博热点");
  }else if(udc%3==1){
    display.drawInvertedBitmap(75, 16, toutiao, 32, 32, GxEPD_BLACK);//画图
    u8g2Fonts.setCursor(106,38);
    u8g2Fonts.print("央视新闻");
    
  }else{
    display.drawInvertedBitmap(75, 16, zhihu, 32, 32, GxEPD_BLACK);//画图
    u8g2Fonts.setCursor(106,38);
    u8g2Fonts.print("知乎热榜");
  }
  String news_title,tt="";
  int maxNewsdispNun = 10;
  int perNewsHigh    = 24;
  int calendarNum    = 0;
  for(int index=0; index<3; index++) {
    if(getCalendarInfo(index).length() > 0)
    {
      calendarNum ++;
    }
  }
  if(calendarNum > 0)
  {
    if(calendarNum == 3)
    {
      maxNewsdispNun = 8;
    }
    perNewsHigh = 20;
  }
  for(int i=0;i<maxNewsdispNun;i++){
    u8g2Fonts.setCursor(4,60+i*perNewsHigh);
    news_title =String(newspaper.title[i]);
    if(news_title.length()>54){
       for(int j=0;j<54;j++){
         tt+=news_title[j];
       }
       u8g2Fonts.print(tt);
     }else{
      u8g2Fonts.print(news_title);
     }
    tt="";
  }
  
}

void calendarInfodisplay(){
    u8g2Fonts.setFont(chinese_city_gb2312);
    u8g2Fonts.setForegroundColor(GxEPD_RED);  // 设置前景色
    u8g2Fonts.setBackgroundColor(GxEPD_WHITE);  // 设置背景色
    String cfgCalendarInfo, dispCalendarInfo;
  int calendarBaseDispPos = 230;
  int perCalendarHigh     = 27;
  int calendarNum         = 0;
  int dispCalendarIndex   = 0;
  for(int index=0; index<3; index++) {
    if(getCalendarInfo(index).length() > 0)
    {
      calendarNum ++;
    }
  }
  if(calendarNum > 0)
  {
    if(calendarNum == 1)
    {
      calendarBaseDispPos = 280;
      perCalendarHigh     = 30;
    }
    else if(calendarNum == 2)
    {
      calendarBaseDispPos = 260;
      perCalendarHigh     = 25;
    }
  }
    for(int index=0; index<3; index++) {
        cfgCalendarInfo = getCalendarInfo(index);
        if(cfgCalendarInfo.length() > 0)
        {
#define LIMIT_CALENDAR_LEN   ( 32 )
            if(cfgCalendarInfo.length()> LIMIT_CALENDAR_LEN){
                String temp = "";
                for(int j=0; j<LIMIT_CALENDAR_LEN; j++){
                    temp += cfgCalendarInfo[j];
                }
                temp[LIMIT_CALENDAR_LEN-1] = '~';
                cfgCalendarInfo = temp;
            }
            dispCalendarInfo = "日程"+String(index+1)+": "+cfgCalendarInfo;
            u8g2Fonts.setCursor(4, calendarBaseDispPos+(dispCalendarIndex++)*perCalendarHigh);
            u8g2Fonts.print(dispCalendarInfo);
        }
    }
}

/*温湿度*/
Adafruit_SHT31 sht30;
bool    enableHeater = false;
uint8_t loopCnt = 0;
int SHT3x_Init(uint16_t timeout)
{
  uint16_t wait;
  Serial.println("SHT30 Init");
  if (! sht30.begin()) {   // Set to 0x45 for alternate i2c addr
    Serial.println("SHT30 Couldn't find");
    while (1)
    {
      if(wait >= timeout) break;
      delay(10);
      wait += 10;
    }
  }

  // Toggle heater enabled state every 30 seconds
  // An ~3.0 degC temperature increase can be noted when heater is enabled
  if (++loopCnt == 10) {
    enableHeater = !enableHeater;
    sht30.heater(enableHeater);
    loopCnt = 0;
  }
  Serial.print("SHT30 Heater Enabled State: ");
  if (sht30.isHeaterEnabled())
    Serial.println("ENABLED");
  else
    Serial.println("DISABLED");
}

void getTempAndHumi(){
  SHT3x_Init(3000);
  float temp = sht30.readTemperature();
  float humi = sht30.readHumidity();
  if (! isnan(temp)) {  // check if 'is not a number'
    Serial.print("SHT30 Temp *C = "); Serial.print(temp); Serial.print("\t\t");
  } else { 
    Serial.println("SHT30 Failed to read  temperature");
  }
  
  if (! isnan(humi)) {  // check if 'is not a number'
    Serial.print("SHT30 Hum. % = "); Serial.println(humi);
  } else { 
    Serial.println("SHT30 Failed to read humidity");
  }

  String temperature = (String)((temp > 0.0) ? (int)(temp+0.5) : (int)(temp-0.5)) + "C";
  String humidity    =  (String)((int)(humi+0.5)) + "%";

  u8g2Fonts.setFont(chinese_city_gb2312);
  u8g2Fonts.setForegroundColor(GxEPD_BLACK);  // 设置前景色
  u8g2Fonts.setBackgroundColor(GxEPD_WHITE);  // 设置背景色
  u8g2Fonts.setCursor(272,222);
  u8g2Fonts.print("室内");
  display.drawInvertedBitmap(312, 210, wendu, 16, 16, (temp > 32.0 || temp < 16.0)? GxEPD_RED : GxEPD_BLACK);//画图
  u8g2Fonts.setCursor(330,222);
  u8g2Fonts.print(temperature);
  display.drawInvertedBitmap(358, 210, shidu, 16, 16, (humi > 80.0 || humi < 30.0)? GxEPD_RED : GxEPD_BLACK);//画图
  u8g2Fonts.setCursor(376,222);
  u8g2Fonts.print(humidity);
}

void getSSIDAndIp()
{
  IPAddress ip;
  String ssidinfo = "";
  if (settingMode) {
    ip   = WiFi.softAPIP();
    ssidinfo = "AP:" + String(apSSID);
  }
  else {
    ip = WiFi.localIP();
    ssidinfo = "WIFI:" + cnSSID;
  }
#define LIMIT_SSID_LEN   ( 22 )
  if(ssidinfo.length()> LIMIT_SSID_LEN){
    String temp = "";
    for(int j=0; j<LIMIT_SSID_LEN; j++){
        temp += ssidinfo[j];
    }
    temp[LIMIT_SSID_LEN-1] = '~';
    ssidinfo = temp;
  }
  Serial.println(ssidinfo + " IP:" + ip.toString());
  u8g2Fonts.setFont(chinese_city_gb2312);
  u8g2Fonts.setForegroundColor(GxEPD_BLACK);  // 设置前景色
  u8g2Fonts.setBackgroundColor(GxEPD_WHITE);  // 设置背景色
  u8g2Fonts.setCursor(ssidinfo.length()> 20 ? 10 : 40, 298);
  u8g2Fonts.print(ssidinfo + " IP:" + ip.toString());
  //u8g2Fonts.setCursor(272,296);
  //u8g2Fonts.print(ip.toString());
}

uint32_t simpleVBatmV(uint32_t simplpe_time)
{
#define VBAT_CTRL_PIN        ( 25 )
#define DEFAULT_VREF         ( 4096 ) //2^ADC_WIDTH_BIT_12
  adc_atten_t    atten   = ADC_ATTEN_11db;    //满量程为3.6v
  adc1_channel_t channel = ADC1_CHANNEL_0;   //SENSOR_VP GPIO36
  //adc1_channel_t channel = ADC1_CHANNEL_3;   //SENSOR_VN GPIO39
  adc1_config_width(ADC_WIDTH_12Bit);         //采集宽度
  adc1_config_channel_atten(channel, atten); //配置通道 以及衰减度

  esp_adc_cal_characteristics_t adc_chars;
  esp_adc_cal_value_t val_type = esp_adc_cal_characterize(ADC_UNIT_1, atten, ADC_WIDTH_12Bit, DEFAULT_VREF, &adc_chars);//在特定衰减下表征ADC的特性
  Serial.println(val_type);

  pinMode(VBAT_CTRL_PIN , OUTPUT);
  digitalWrite(VBAT_CTRL_PIN, HIGH); //open vbat sample
  delay(100);
  adc1_get_raw(channel);
  //Multisampling
  uint32_t adc_reading = 0;
  uint32_t times       = 0;
  uint32_t waitms      = 0;
  for (times = 0, waitms = 0; waitms < simplpe_time; times++) {
    adc_reading += adc1_get_raw(channel); //从单个通道获取ADC1读数
    delay(10);
    waitms += 10;
  }
  if(times > 0)
  {
    adc_reading /= times;
  }

  //Convert adc_reading to voltage in mV
  uint32_t voltage = esp_adc_cal_raw_to_voltage(adc_reading, &adc_chars); //将ADC读数转换为以mV为单位的电压
  Serial.printf("VBat Raw: %d, Voltage: %dmV\r\n", adc_reading, voltage);

  digitalWrite(VBAT_CTRL_PIN, HIGH); //close vbat sample

  return voltage;
}

float voltageConvToQuantily(uint32_t voltage)
{
  float quantily_f;
#if 1
typedef struct{
  uint32_t voltage;
  uint32_t quantily;
}VBAT_INFO_TABLE;
  const VBAT_INFO_TABLE vbatTable[] = 
  {
    {4220,100},
    //{4200,100},
    {4060, 90},
    {3980, 80},
    {3920, 70},
    {3780, 60},
    {3820, 50},
    {3790, 40},
    {3770, 30},
    {3740, 20},
    {3680, 10},
    {3450,  5},
    {3060,  0},
    //{3000,  0},
  };
#define vbatTableAllNums  (sizeof(vbatTable)/sizeof(vbatTable[0]))
  VBAT_INFO_TABLE onHighVbat = vbatTable[vbatTableAllNums-1];
  VBAT_INFO_TABLE onLowVbat = vbatTable[vbatTableAllNums-1];
  for(int i=0; i<vbatTableAllNums; i++)
  {
    if(voltage >= vbatTable[i].voltage)
    {
      onLowVbat = vbatTable[i];
      if(i!=0)
      {
        onHighVbat = vbatTable[i-1];
      }
      else
      {
        onHighVbat = vbatTable[i];
      }
      break;
    }
  }
  if(onHighVbat.quantily == onLowVbat.quantily)
  {
    quantily_f = onHighVbat.quantily;
  }
  else
  {
    //Y = (A-B)/(a-b)*(V-b) + B
    quantily_f = ((float)voltage-onLowVbat.voltage)*(onHighVbat.quantily-onLowVbat.quantily) /(onHighVbat.voltage-onLowVbat.voltage)+ onLowVbat.quantily;
  }
#else
#define VBAT_MAX_VOL_MV    (4260.0f) //100%
#define VBAT_MIN_VOL_MV    (3600.0f) //0%
  if(voltage > VBAT_MIN_VOL_MV)
  {
    quantily_f = ((float)voltage-VBAT_MIN_VOL_MV)/(VBAT_MAX_VOL_MV-VBAT_MIN_VOL_MV)*100.0; // %
    Serial.println("quantily_f: " + String(quantily_f) + "%");
    quantily_f = quantily_f > 100.0 ? 100 : quantily_f;
  }
  else
  {
    quantily_f = 0;
  }
#endif
  return quantily_f;
}
void getVBat()
{
  String vbatVoltage = "电池电压  ";
  String vbatQuantity = "电量  ";
  #define VBAT_SAMPLE_TIME_MS  200
  uint32_t voltage = simpleVBatmV(VBAT_SAMPLE_TIME_MS);
  voltage = voltage*2; //电阻等分
  float quantily_f = voltageConvToQuantily(voltage);
  uint32_t quantily = uint32_t(quantily_f + 0.3f);
  Serial.println(vbatVoltage + String(voltage) + "mV");
  Serial.println(vbatQuantity + String(quantily) + "%");
 
  u8g2Fonts.setFont(chinese_city_gb2312);
  u8g2Fonts.setForegroundColor(GxEPD_BLACK);   //设置前景色
  u8g2Fonts.setBackgroundColor(GxEPD_WHITE);   //设置背景色
#if 0
  //电池电压: 
  u8g2Fonts.setCursor(156, 298);
  u8g2Fonts.print(vbatVoltage);
#endif
  //电量: 
  u8g2Fonts.setCursor(272, 298);
  u8g2Fonts.print(vbatQuantity);
 
  u8g2Fonts.setFont(chinese_city_gb2312);
  if(quantily <= 20) //低电压 20% 红色显示
  {
    u8g2Fonts.setForegroundColor(GxEPD_RED);   //设置前景色
  }
  else
  {
    u8g2Fonts.setForegroundColor(GxEPD_BLACK);   //设置前景色
  }
  u8g2Fonts.setBackgroundColor(GxEPD_WHITE);   //设置背景色
#if 0
  //电池电压: 
  u8g2Fonts.setCursor(156+(12*4+8*2), 298);
  u8g2Fonts.print(String(voltage) +"mV");
#endif
  //电量: 
  u8g2Fonts.setCursor(272+(12*2+8*2), 298);
  u8g2Fonts.print(String(quantily) + "%" + "  " + String(voltage) + "mV");
  //u8g2Fonts.print(String(quantily) + "%");
}
